<?php

/**
 * SQL对象化
 */
/**
 * CDbCriteria class file.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
 * @copyright 2008-2013 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
/**
 * CDbCriteria represents a query criteria, such as conditions, ordering by, limit/offset.
 *
 * It can be used in AR query methods such as CActiveRecord::find and CActiveRecord::findAll.
 *
 * $criteria=new CDbCriteria();
 * $criteria->compare('status',Post::STATUS_ACTIVE);
 * $criteria->addInCondition('id',array(1,2,3,4,5,6));
 *
 * $posts = Post::model()->findAll($criteria);
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package system.db.schema
 * @since 1.0
 */

namespace library\Core\Db;


class CDbCriteria {

    /**
     * Constructor.
     * @param array $data criteria initial property values (indexed by property name)
     */
    public function __construct() {
        
    }

    private $_AR_FUNCTION_CALLED_ = array();
    private $_specialMethods = array('select', 'insert', 'update', 'delete');

    public function getCriteriaBuilder(CDbTable $table) {


        $self = new CDbCriteriaBuilder();

        foreach ($this->_AR_FUNCTION_CALLED_ as $k => $method) {

            $parameter = $method[1];

            if (in_array($method[0], $this->_specialMethods)) {
                array_unshift($parameter, $table);
            }

            call_user_func_array(array($self, $method[0]), $parameter);
        }
        return $self;
    }

    public function from(CDbTable $table, $fields) {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    public function select($fields) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    public function insert($data) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    public function update($data) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    public function delete() {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * Appends a search condition to the existing {@link condition}.
     * The search condition and the existing condition will be concatenated via the specified operator
     * which defaults to 'AND'.
     * The search condition is generated using the SQL LIKE operator with the given column name and
     * search keyword.
     * @param string $column the column name (or a valid SQL expression)
     * @param string $keyword the search keyword. This interpretation of the keyword is affected by the next parameter.
     * @param boolean $escape whether the keyword should be escaped if it contains characters % or _.
     * When this parameter is true (default), the special characters % (matches 0 or more characters)
     * and _ (matches a single character) will be escaped, and the keyword will be surrounded with a %
     * character on both ends. When this parameter is false, the keyword will be directly used for
     * matching without any change.
     * @param string $operator the operator used to concatenate the new condition with the existing one.
     * Defaults to 'AND'.
     * @param string $like the LIKE operator. Defaults to 'LIKE'. You may also set this to be 'NOT LIKE'.
     * @return CDbCriteria the criteria object itself
     */
    public function addSearchCondition($column, $keyword, $escape = true, $operator = 'AND', $like = 'LIKE') {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * Appends an IN condition to the existing {@link condition}.
     * The IN condition and the existing condition will be concatenated via the specified operator
     * which defaults to 'AND'.
     * The IN condition is generated by using the SQL IN operator which requires the specified
     * column value to be among the given list of values.
     * @param string $column the column name (or a valid SQL expression)
     * @param array $values list of values that the column value should be in
     * @param string $operator the operator used to concatenate the new condition with the existing one.
     * Defaults to 'AND'.
     * @return CDbCriteria the criteria object itself
     */
    public function addInCondition($column, $values, $operator = 'AND') {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * Appends an NOT IN condition to the existing {@link condition}.
     * The NOT IN condition and the existing condition will be concatenated via the specified operator
     * which defaults to 'AND'.
     * The NOT IN condition is generated by using the SQL NOT IN operator which requires the specified
     * column value to be among the given list of values.
     * @param string $column the column name (or a valid SQL expression)
     * @param array $values list of values that the column value should not be in
     * @param string $operator the operator used to concatenate the new condition with the existing one.
     * Defaults to 'AND'.
     * @return CDbCriteria the criteria object itself
     * @since 1.1.1
     */
    public function addNotInCondition($column, $values, $operator = 'AND') {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * Appends a condition for matching the given list of column values.
     * The generated condition will be concatenated to the existing {@link condition}
     * via the specified operator which defaults to 'AND'.
     * The condition is generated by matching each column and the corresponding value.
     * @param array $columns list of column names and values to be matched (name, value)
     * @param string $columnOperator the operator to concatenate multiple column matching condition. Defaults to 'AND'.
     * @param string $operator the operator used to concatenate the new condition with the existing one.
     * Defaults to 'AND'.
     * @return CDbCriteria the criteria object itself
     */
    public function addColumnCondition($columns, $columnOperator = 'AND', $operator = 'AND') {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * Adds a between condition to the {@link condition} property.
     *
     * The new between condition and the existing condition will be concatenated via
     * the specified operator which defaults to 'AND'.
     * If one or both values are empty then the condition is not added to the existing condition.
     * This method handles the case when the existing condition is empty.
     * After calling this method, the {@link condition} property will be modified.
     * @param string $column the name of the column to search between.
     * @param string $valueStart the beginning value to start the between search.
     * @param string $valueEnd the ending value to end the between search.
     * @param string $operator the operator used to concatenate the new condition with the existing one.
     * Defaults to 'AND'.
     * @return CDbCriteria the criteria object itself
     * @since 1.1.2
     */
    public function addBetweenCondition($column, $valueStart, $valueEnd, $operator = 'AND') {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 小于
     * @param unknown $column
     * @param unknown $value
     * @param string $operator
     */
    public function addLtCondition($column, $value, $operator = 'AND') {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 小于等于
     * @param unknown $column
     * @param unknown $value
     * @param string $operator
     * @return CDbCriteria
     */
    public function addLteCondition($column, $value, $operator = 'AND') {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 大于
     * @param unknown $column
     * @param unknown $value
     * @param string $operator
     * @return CDbCriteria
     */
    public function addGtCondition($column, $value, $operator = 'AND') {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 大于等于
     * @param unknown $column
     * @param unknown $value
     * @param string $operator
     * @return CDbCriteria
     */
    public function addGteCondition($column, $value, $operator = 'AND') {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 不等于
     * @param unknown $column
     * @param unknown $value
     * @param string $operator
     * @return CDbCriteria
     */
    public function addNqCondition($column, $value, $operator = 'AND') {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 等于
     * @param unknown $column
     * @param unknown $value
     * @param string $operator
     * @return CDbCriteria
     */
    public function addEqCondition($column, $value, $operator = 'AND') {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 排序
     * @param string $column
     * @param string $sort
     */
    public function addOrderBy($column, $sort = 'ASC') {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 升序
     * @param string $column
     */
    public function addOrderAsc($column) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 降序
     * @param string $column
     */
    public function addOrderDesc($column) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 排序
     * @param string $column
     * @param string $sort
     */
    public function addGroupBy($column) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 查询以关键字开头
     * @param string $column
     * @param string $keyword
     * @return CDbCriteria
     */
    public function addLikeLCondition($column, $keyword) {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    /**
     * 查询以关键字结尾
     * @param string $column
     * @param string $keyword
     * @return CDbCriteria|CDbCriteria
     */
    public function addLikeRCondition($column, $keyword) {
        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    public function addSelect($fields, $table = '') {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    public function addJoin($joinType, CDbTable $table, $on, $fields = array()) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    public function addLeftJoin(CDbTable $table, $on, $fields = array()) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    public function addRightJoin(CDbTable $table, $on, $fields = array()) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    public function addInnerJoin(CDbTable $table, $on, $fields = array()) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    public function setLimit($limit) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

    public function setOffset($offset) {

        $this->_AR_FUNCTION_CALLED_[] = array(__FUNCTION__, func_get_args());

        return $this;
    }

}
